<?php
defined('IN_YuLin') || exit('NO PERMIT!');
UserLogin(U($m.'/'.$c.'/'.$a));

$userTab      = Table('user');
$infoTab      = Table('user_info');
$invTab       = Table('user_invita_code');
$mainTab      = Table('lottery_order_main');
$itemTab      = Table('lottery_order_item');
$perTab       = Table('lottery_k3');
$lottTab      = Table('lottery');
// 获取所有彩种
$sql          = 'SELECT id, title FROM ' . $lottTab . ' WHERE status = 1';
$lottList     = $db->getall($sql, 'id');

$where        = 'WHERE cd.uid = ' . $_uid;
$lott_id      = NoBadStr($_GET['lott_id']);
$dayline      = NoBadStr($_GET['dayline']);
$staline      = NoBadStr($_GET['staline']);
$username     = NoBadStr($_GET['username']);
if ($lott_id > 0){
    $where .= ' AND main.`lott_id` = ' . $lott_id;
}
if ($dayline == '1'){
    $where .= ' AND main.`createtime` > ' . strtotime(date('Y-m-d'));
}elseif ($dayline == '2'){
    $where .= ' AND main.`createtime` > ' . strtotime(date('Y-m-d', strtotime('-1 day'))) . ' AND main.`createtime` <= ' . strtotime(date('Y-m-d'));
}elseif ($dayline == '3'){
    $where .= ' AND main.`createtime` > ' . strtotime(date('Y-m-d', strtotime('-7 day'))) . ' AND main.`createtime` <= ' . strtotime(date('Y-m-d'));;
}else{
    // 默认当天
    $where .= ' AND main.`createtime` > ' . strtotime(date('Y-m-d'));
}

if ($staline == '2'){
    $where .= ' AND main.`status` = 2';
}elseif ($staline == '3'){
    $where .= ' AND main.`status` = 1';
}elseif ($staline == '4'){
    $where .= ' AND main.`status` = 0';
}

if ($username){
    $where .= ' AND u.`username` = "' . $username . '"';
}

// 载入当前代理所有的邀请码
$sql = 'SELECT main.`id` as p
        FROM ' . $mainTab . ' AS main
        LEFT JOIN ' . $itemTab . ' AS item
        ON main.`id` = item.`o_id`
        LEFT JOIN ' . $userTab . ' AS u
        ON main.`uid` = u.`id`
        LEFT JOIN ' . $infoTab . ' AS info
        ON u.`id` = info.`uid`
        LEFT JOIN ' . $invTab . ' AS cd
        ON info.`invita_code_id` = cd.`id`
        LEFT JOIN ' . $perTab . ' AS per
        ON item.`period_id` = per.`id`
        ' . $where . ' GROUP BY main.`id` ORDER BY main.`createtime` DESC, per.`id` ASC';
$tmp = $db->getall($sql);
if (count($tmp) > 0){
    $sql = '
            SELECT main.`id`, main.`code` AS bet_str, main.`amount`, main.`prize`, main.`createtime`, main.`lott_id`,
        	u.`username`, info.`invita_code_id`,
        	per.`period`, per.`code` AS open_code
        FROM ' . $mainTab . ' AS main
        LEFT JOIN ' . $itemTab . ' AS item
        ON main.`id` = item.`o_id`
        LEFT JOIN ' . $userTab . ' AS u
        ON main.`uid` = u.`id`
        LEFT JOIN ' . $infoTab . ' AS info
        ON u.`id` = info.`uid`
        LEFT JOIN ' . $invTab . ' AS cd
        ON info.`invita_code_id` = cd.`id`
        LEFT JOIN ' . $perTab . ' AS per
        ON item.`period_id` = per.`id`
        ' . $where . ' GROUP BY main.`id` ORDER BY main.`createtime` DESC, per.`id` ASC';
    $page  = max(1,intval($_GET['page']));
    $size  = 20;
    $limit    = ' LIMIT '.($page - 1) * $size.','.$size;
    $url   = '?m='.$m.'&c='.$c;
    $orderby = ' ORDER BY u.id DESC ';
    $data  = $db->getall($sql . $limit);
    $multi = Multi($url,$page,$tmp['p'],$size);
}

$tpl->display('member/agentBetRecord');
